package com.hk.demo.player.grid;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hk.demo.player.MyBaseActivity;
import com.hk.demo.player.R;
import com.hk.demo.player.http.ApiRepository;
import com.hk.demo.player.http.FirmMonitor;
import com.hk.player.PlayerControlView;
import com.hk.player.core.DataCallback;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ScreenOrientationHelper;
import com.zhoug.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

/**
 * @author: zhoug
 * @since: 2025-08-15
 */
public class VideoPagerPlayerActivity extends MyBaseActivity {
    private static final String TAG = ">>>GridPlayerActivity";
    private VideoPagerManager mVideoPagerManager;
    private VideoControlManager mVideoControlManager;

    private TextView mTvHeader;
    private ViewPager2 mViewPager;
    private FrameLayout mVideoContainer;
    private TextView mBtnMode;
    private PlayerControlView mPlayerControlView;

    private ApiRepository mApiRepository;
    private final List<FirmMonitor> mFirmMonitor = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.video_pager_player_activity;
    }

    @Override
    protected void findViews() {
        mTvHeader = findViewById(R.id.tv_header);
        mViewPager = findViewById(R.id.view_pager);
        mVideoContainer = findViewById(R.id.video_container);
        mBtnMode = findViewById(R.id.btn_mode);
        mPlayerControlView = findViewById(R.id.playerControlView);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        LogUtils.d(TAG, "onCreate1:");
        mVideoPagerManager = new VideoPagerManager(this, getLifecycle(), mViewPager);
        mVideoPagerManager.setListener(new VideoPagerManager.Listener() {
            @Override
            public void onGridModeChange(int gridMode) {
                mBtnMode.setText(GridMode.getModeName(gridMode));
            }
        });
        mBtnMode.setOnClickListener(v -> mVideoPagerManager.toggleGridMode());

        mVideoControlManager = new VideoControlManager(this, mPlayerControlView);
        mVideoControlManager.setOnScreenOrientationListener(screenOrientation -> {
            if (screenOrientation == ScreenOrientationHelper.ORIENTATION_LANDSCAPE) {
                //横屏
                mTvHeader.setVisibility(View.GONE);
                ViewGroup.LayoutParams layoutParams = mVideoContainer.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mVideoContainer.setLayoutParams(layoutParams);
            } else if (screenOrientation == ScreenOrientationHelper.ORIENTATION_PORTRAIT) {
                //竖屏
                mTvHeader.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams layoutParams = mVideoContainer.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                mVideoContainer.setLayoutParams(layoutParams);
            }
        });


        loadData();
    }


    private void loadData() {
        mApiRepository = new ApiRepository();
        mApiRepository.getPreviewMonitorList(1, 100, new DataCallback<List<FirmMonitor>>() {
            @Override
            public void onSuccess(List<FirmMonitor> firmMonitors) {
                if (firmMonitors != null && !firmMonitors.isEmpty()) {
                    mFirmMonitor.clear();
                    mFirmMonitor.addAll(firmMonitors);
                } else {
                    ToastUtils.toastLong("没有获取到摄像头");
                }
                mVideoPagerManager.setData(FirmMonitor.toMonitorData(firmMonitors));
            }

            @Override
            public void onFailure(String error) {
                ToastUtils.toastLong("获取摄像头失败:" + error);
            }
        });
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mVideoControlManager != null) {
            mVideoControlManager.onConfigurationChanged(newConfig);
        }
    }
}
